package com.fw.ui.util;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.server.Page;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.WebBrowser;
import com.vaadin.ui.UI;

public class ApplicationHelper {

    public static ApplicationContext getApplicationContext() {
	return getApplicationContext(getCurrentApplication());
    }

    public static ApplicationContext getApplicationContext(UI app) {
	ServletContext sc = VaadinServlet.getCurrent().getServletContext();
	return WebApplicationContextUtils.getWebApplicationContext(sc);
    }

    public static WebBrowser getBrowser() {
	return Page.getCurrent().getWebBrowser();
    }

    public static HttpServletRequest getRequest() {
	ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	if (attrs != null) {
	    return attrs.getRequest();
	}
	return null;
    }

    public static UI getCurrentApplication() {
	return ApplicationHolder.getApplication();
    }

    public static Locale getCurrentLocale() {
	return getCurrentApplication().getLocale();
    }

    public static String getMessage(String key) {
	return getVaadinApplicationObjectSupport().getMessage(key);
    }

    public static String getMessage(String key, String... args) {
	return getVaadinApplicationObjectSupport().getMessage(key, args);
    }

    public static VaadinApplicationObjectSupport getVaadinApplicationObjectSupport() {
	String names[] = getApplicationContext().getBeanNamesForType(VaadinApplicationObjectSupport.class);
	if (names != null && names.length == 1) {
	    return (VaadinApplicationObjectSupport) getApplicationContext().getBean(names[0]);
	}
	return null;
    }

    public static String getWebInfPath() {
	return VaadinServlet.getCurrent().getServletContext().getRealPath("\\WEB-INF\\");
    }

}
